import { observable, action } from 'mobx';
import {WeaForm} from 'comsMobx'
import {WeaLocaleProvider} from "ecCom"
import {Modal,message} from "antd"
import { toJS } from 'mobx';
import * as Apis from '../../apis/prjPortal';
import * as Prj_Apis from '../../apis/project'
import { TaskInfoStore } from '../task/taskInfoStore'
import {ProjectInfoStore } from '../projectInfoStore';
import {PrjCardStore } from '../prjCardStore';
import {TaskListStore } from '../single/task/taskListStore';
const getLabel = WeaLocaleProvider.getLabel;

class ProjectCardStore{
    @observable prjid="";

    @observable prjids="";

    @observable createInfo="";

    @observable tasknum="";

    @observable membernum="";

    @observable exchangeList = {};

    @observable _BrowserList = {};

    @observable logList = [];

    @observable daily = "";

    @observable dailys = [];

    @observable reply = "";

	@observable alldatas = [];
	
	@observable baseInfo = {
		createdate: "",
		creater: "",
		creatername: "",
		manager: "",
		managericons: "",
		managername: "",
		members: 0,
		planbegindate: "",
		planenddate: "",
		stages : []
	}

	@observable statistics = {
		jxz: "",
		rwzs: "",
		wcl: 0,
		wks: "",
		xmjd: 0,
		yq: "",
		yql: 0,
		ywc: "",
	}

	@observable projectInfo = {};

	@observable loading = false;
	@observable apiErr = false;
	@observable topTab = [];

	@observable taskInfoStore = new TaskInfoStore();

	@observable canEdit = 0;

	@observable canSubmitDaily = false;

	@observable rightMenu = [];

	@observable isApprove = false;

	@observable projectInfoStore = new ProjectInfoStore();

	@observable templatename = '';

	@observable doSaveVisible = false;

	@observable prjCardStore = new PrjCardStore();

	@observable taskListStore = new TaskListStore();

	@observable status = 0;

	@action
	initDatas = () => {
		// Apis.getPortalProjectCardInfo(params).then(data=>{
		//     this.createInfo = data.createInfo;
		//     this.tasknum = data.tasknum;
		//     this.membernum = data.membernum;
		// })
		this.getPortalProjectInfo();
        this.getPortalProjectDynamicInfo("all");
    }

	@action
	setPrjid = (v) => {
		this.setPrjidFun(v);
		this.initDatas();
	}
	setPrjidFun = (v) => {
		this.prjid = v;
	}

    //获取相关交流信息
	@action
	getExchangeList = () => {
        let _this = this;
		const newParams = { types: "PP", sortid: this.prjid };
		Prj_Apis.getExchangeList(newParams).then(data => {
            let exchange = { ...data }
			exchange.datas.forEach((element, index) => {
				element.isEdit = false
			})
			exchange.datas.unshift({
				id: 'create',
				isEdit: true,
				remark: '',
				docids: [],
				projectids: [],
				relatedcus: [],
				relateddoc: [],
				relatedprj: [],
				relatedwf: [],
			})
            _this.exchangeList = exchange;
			_this._BrowserList = {
				'create': {remark: ''},
				datas: [],
			};
			_this.exchangeList.datas&&_this.exchangeList.datas.forEach((element, index) => {
				if (_this._BrowserList[element['id']] == undefined) {
					_this._BrowserList[element['id']] = {};
				}
				_this._BrowserList[element['id']].remark = element.remark;
				_this._BrowserList[element['id']].docids = element.docids;
				_this._BrowserList[element['id']].projectids = element.projectids;
				_this._BrowserList[element['id']].relatedcus = element.relatedcus;
				_this._BrowserList[element['id']].relateddoc = element.relateddoc;
				_this._BrowserList[element['id']].relatedprj = element.relatedprj;
				_this._BrowserList[element['id']].relatedwf = element.relatedwf;
			})
			_this._BrowserList.datas = _this.exchangeList.datas;
		})
	}
	
	@action
	getPortalProjectInfo = (params = {}) => {
		let _this = this;
		const newParams = { prjid: this.prjid, ...params };
		this.loading = true;
		Apis.getPortalProjectInfo(newParams).then(data => {
			const { baseInfo, statistics } = data
			const { createdate, creater, creatername, manager, managericons, managername, members, planbegindate, planenddate, stages } = baseInfo
			const { jxz, rwzs, wcl, wks, xmjd, yq, yql, ywc } = statistics
			this.baseInfo = {
				createdate: createdate,
				creater: creater,
				creatername: creatername,
				manager: manager,
				managericons: managericons,
				managername: managername,
				members:members,
				planbegindate: planbegindate,
				planenddate: planenddate,
				stages : stages
			}
			this.statistics = {
				jxz: jxz,
				rwzs:rwzs,
				wcl: wcl,
				wks: wks,
				xmjd: xmjd,
				yq: yq,
				yql: yql,
				ywc: ywc,
			}

			this.projectInfo = data.projectInfo;
			this.topTab = data.topTab;
			this.apiErr = false;
			this.canEdit = data.canEdit;
			this.rightMenu = data.rightMenus;
			this.isApprove = data.isApprove;
			this.status = data.status;
		})
			.catch(action(err => { this.apiErr = true }))
			.finally(action(() => { this.loading = false; }));
	}
    
    //获取项目动态信息
	@action
	getPortalProjectDynamicInfo = (type) => {
        let _this = this;
		const newParams = {type:type,prjid:this.prjid};
		Apis.getPortalProjectDynamicInfo(newParams).then(data => {
			if (type == "all") {
				_this.alldatas = data;
				_this.exchangeList = data;
				_this._BrowserList = {
					'create': { remark: '' },
					datas: [],
				};
				_this.exchangeList.datas && _this.exchangeList.datas.forEach((element, index) => {
					if (_this._BrowserList[element['id']] == undefined) {
						_this._BrowserList[element['id']] = {};
					}
					_this._BrowserList[element['id']].remark = element.remark;
					_this._BrowserList[element['id']].docids = element.docids;
					_this._BrowserList[element['id']].projectids = element.projectids;
					_this._BrowserList[element['id']].relatedcus = element.relatedcus;
					_this._BrowserList[element['id']].relateddoc = element.relateddoc;
					_this._BrowserList[element['id']].relatedprj = element.relatedprj;
					_this._BrowserList[element['id']].relatedwf = element.relatedwf;
				})
				_this._BrowserList.datas = toJS(data.datas);
			} else if (type == "log") {
				this.logList = data.datas;
			}
			this.apiErr = false;
		})
			.catch(action(err => { this.apiErr = true }))
			.finally(action(() => { this.loading = false; }));
	}

    setExchangeList = (data) => {
		this.exchangeList = data;
    }
    
	setBrowserList = (_BrowserList) => {
		this._BrowserList = _BrowserList;
    }
    
    //相关交流datas循环
	changeDiscussList = (value = {}) => {
		this.exchangeList = value;
		this.exchangeList.datas&&this.exchangeList.datas.forEach((element, index) => {
			if (this._BrowserList[element['id']] == undefined) {
				this._BrowserList[element['id']] = {};
			}
			this._BrowserList[element['id']].remark = element.remark;
			this._BrowserList[element['id']].docids = element.docids;
			this._BrowserList[element['id']].projectids = element.projectids;
			this._BrowserList[element['id']].relatedcus = element.relatedcus;
			this._BrowserList[element['id']].relateddoc = element.relateddoc;
			this._BrowserList[element['id']].relatedprj = element.relatedprj;
			this._BrowserList[element['id']].relatedwf = element.relatedwf;
		})
		this._BrowserList.datas = this.exchangeList.datas;
    }
    
    //相关交流提交
	exchangeSave = (value) => {
		let _this = this;
		Prj_Apis.exchangeSave({...value,discusstype:'PP'}).then(data => {
			if(data.status){
				_this.getExchangeList({ });
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
    }
    
    //删除
	exchangeDelete = (value) => {
		let _this = this;
		Prj_Apis.exchangeDelete(value).then(data => {
			if(data.status){
				message.success(getLabel(83472, "删除成功！"));
				_this.getExchangeList();
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
	
	//删除日报
	deleteDaily = (v,tabKey) =>{
		let _this = this;
		Apis.doPortalProjectDailyOperation({...v,type:'5'}).then(data => {
			if(data.success){
				message.success(getLabel(83472, "删除成功！"));
				if(tabKey==0){
					_this.getPortalProjectDynamicInfo("all");
				}else{
					_this.getDailys({});
				}
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
    
    setDaily = (v) =>{
        this.daily = v;
    }

	//提交日报
	submitDaily = () => {
		let _this = this;
		Apis.doPortalProjectDailyOperation({ prjid: this.prjid, type: '1', content: this.daily }).then(data => {
			if (data.success) {
				this.loading = true;
				_this.getDailys({});
			} else {
				message.error(data.error ? data.error : `${getLabel(384137, '操作失败！')}`);
			}
		})
		.catch((err) => { throw new Error(`system_error:${err}`) })
		.finally(() => {this.loading = false; })
	}

	//编辑日报
	editDaily = (id,text,tabKey) => {
		let _this = this;
		Apis.doPortalProjectDailyOperation({ dailyid: id, type: '6', content: text }).then(data => {
			if (data.success) {
				this.loading = true;
				if(tabKey==0){
					_this.getPortalProjectDynamicInfo("all");
				}else{
					_this.getDailys({});
				}
			} else {
				message.error(data.error ? data.error : `${getLabel(384137, '操作失败！')}`);
			}
		})
		.catch((err) => { throw new Error(`system_error:${err}`) })
		.finally(() => {this.loading = false; })
	}

    //获取日报数据
    getDailys = () =>{
		Apis.getPortalProjectDailyDatas({prjid:this.prjid,type:'prj'}).then(data => {
			this.dailys = data.dailys;
			this.canSubmitDaily = data.canSubmitDaily;
		})
    }

    setReply = (v) =>{
        this.reply = v;
    }

    saveReply = (dailyid,type) =>{
        let _this = this;
		Apis.doPortalProjectDailyOperation({prjid:this.prjid,type:'2',dailyid:dailyid,content:this.reply}).then(data => {
			if(data.success){
				if(type=="all"){
                    _this.getPortalProjectDynamicInfo("all");
                }else if(type=="daily"){
                    _this.getDailys({ });
                }
			}else {
				message.error(data.error ? data.error : `${getLabel(384137,'操作失败！')}`);
			}
		})
	}
	
	//项目执行操作
	@action
	doPlanOpt = (params = {}) => {
		Prj_Apis.doPlanOpt(params).then(data => {
			this.getPortalProjectInfo();
			if(params.callBack){
				params.callBack();
			}
		});
	}

	@action
	setVisiable = (bool) => {
		this.templatename=this.projectInfo.prjname;
        this.doSaveVisible = bool;
	}

	@action
	getRightMenu = (params) =>{
		Apis.getRightMenu({prjid:this.prjid,...params}).then(data => {
			this.rightMenu = data.rightMenus;
			this.status = data.status;
		})
	}

}

const projectCardStore = new ProjectCardStore();
export default projectCardStore;